<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title"
    width="30%"
    :before-close="handleClose"
  >
    <div>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="类型名称" prop="name">
          <el-input
            placeholder="输入类型名称"
            style="width: 100%"
            v-model="ruleForm.name"
          />
        </el-form-item>

        <el-form-item label="是否默认分类" prop="is_default">
          <el-radio-group v-model="ruleForm.is_default">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group
            v-model="ruleForm.status"
            :disabled="ruleForm.id ? true : false"
          >
            <el-radio :label="1">有效</el-radio>
            <el-radio :label="0">无效</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="客保数量" prop="customer_num">
          <el-input
            placeholder="客保数量"
            style="width: calc(100% - 50px)"
            v-model="ruleForm.customer_num"
          />
          <span style="font-size: 12px; color: #a1a2a3; margin-left: 10px">
            *0不限
          </span>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="submit"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { addApi, saveApi } from "@/plugins/api/customer/setup/first";

let ruleForm = ref({});
let rules = {
  name: [{ required: true, message: "请输入类型名称", trigger: "blur" }],
  is_default: [
    { required: true, message: "选择是否默认分类", trigger: "change" },
  ],
  status: [{ required: true, message: "选择是否默认分类", trigger: "change" }],
  customer_num: [
    { required: true, message: "请输入客保数量", trigger: "change" },
  ],
};
let dialogVisible = ref(false);
function handleClose(done) {
  ElMessageBox.confirm("是否确定关闭此对话框?")
    .then(() => {
      done();
    })
    .catch(() => {});
}
let title = ref("");
let ruleFormRef = ref(null);
function initFun(type, row) {
  if (ruleFormRef.value) {
    ruleFormRef.value.resetFields();
  }
  if (type == "add") {
    title.value = "新增类型";
    ruleForm.value = {};
    ruleForm.value.is_default = 1;
    ruleForm.value.status = 1;
  } else {
    title.value = "编辑类型";
    ruleForm.value = Object.assign({}, row);
  }

  dialogVisible.value = true;
}
const emit = defineEmits(["refreshList"]);
function submit() {
  if (!ruleFormRef.value) return;
  ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      if (ruleForm.value.id) {
        saveApi(ruleForm.value).then(() => {
          ElMessage.success("编辑成功！");
          dialogVisible.value = false;
          emit("refreshList");
        });
      } else {
        addApi(ruleForm.value).then((data) => {
          ElMessage.success("新增成功！");
          dialogVisible.value = false;
          emit("refreshList");
        });
      }
    }
  });
}
defineExpose({
  initFun,
});
</script>
<style scoped lang="scss"></style>
